/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sampleappa;

import com.ericsson.nfc.signing.FetcherException;
import com.ericsson.nfc.signing.SignatureFetcher;
import com.ericsson.nfc.signing.record.URIRecord;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;

/**
 *
 * @author emarkki
 */
public class MySignatureWebFetcher implements SignatureFetcher {

    public byte[] fetchSignature(URIRecord uri) throws FetcherException {
        
        // TODO: Security: limit what types of URI:s that are allowed?
        
        InputStream in = null;
        try {
            in = Connector.openInputStream(uri.getURI());
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int ch;
            while ((ch = in.read()) != -1) {
                out.write(ch);
            }

            return out.toByteArray();
        } catch (IOException ex) {
            ex.printStackTrace();
            throw new FetcherException("IOException: " + ex.getMessage(), ex);
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
    
}
